package com.qf.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.Menu;
import com.qf.entity.Role;
import com.qf.entity.User;
import com.qf.enums.Page;
import com.qf.mapper.UserMapper;
import com.qf.service.UserService;
import com.qf.utils.Md5Util;

import com.qf.utils.MenuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;



    @Override
    public User findByUsername(String username) {
        //根据用户名查找用户信息
        return userMapper.findUserByUsername(username);
    }

    //角色一二级菜单
    @Override
    public Role findRole(Integer userId) {
        //根据用户id查询角色信息
        Role role = userMapper.findRoleByUserId(userId);
        //根据角色id查询菜单信息
        List<Menu> menus = userMapper.findMenuByRoleId(role.getRoleId());

        //2.1 对菜单列表分级
       /* ArrayList firstMenu= new ArrayList();//保存一级菜单
        for (Menu menu : menus) {
            if(menu.getSuperMenuId()==0){ //一级菜单
                ArrayList secondList= new ArrayList();//保存二级菜单
                for (Menu second : menus) {
                    if(menu.getMenuId()==second.getSuperMenuId()){//如果一级菜单的id(menuid)==二级菜单的父id(upmenuid)
                        secondList.add(second);
                    }
                }
                //将二级菜单的集合封装到一级菜单中
                menu.setMenus(secondList);
                //将一级菜单在添加到集合中
                firstMenu.add(menu);
            }
        }*/
        List<Menu> firstMenu = MenuUtil.firstMenu(menus);
        //3.将菜单赋值给角色
        role.setMenus(firstMenu);
        return role;
    }



    //分页 模糊查
    @Override
    public PageInfo<User> findAllUsers(Map map, Integer page) {

        //固定长度是5
        Integer size= Page.pageSize.getSize();

        //开始页 每页显示条数
        PageHelper.startPage(page,size);

        List<User> allUsers = userMapper.findAllUsers(map);

        PageInfo<User> userPageInfo = new PageInfo<>(allUsers);;

        return userPageInfo;
    }


    //改变用户状态
    @Transactional
    @Override
    public int setUserStausService(Integer userId) {
        int i = userMapper.setUserStaus(userId);
        if (i>0){
            return 1;
        }
        return 0;
    }

    //启用
    @Override
    public int setUserStausActive(Integer userId) {
        int i = userMapper.setUserStausActive(userId);

        if (i>0){
            return 1;
        }
        return 0;
    }

    //添加用户
    @Transactional
    @Override
    public int addUser(User user) {

        if (user.getPicAddress()==null){
            user.setPicAddress("http://");
        }
        String password = user.getPassword();
        String salt = Md5Util.uuidRandom();
        String md5Password = Md5Util.getMd5Password(salt, password);
        user.setPassword(md5Password);
        user.setSalt(salt);

        System.out.println(user);
       /* HashMap<String, Object> map = new HashMap<>();
        map.put("username",user.getUsername());
        map.put("password",user.getPassword());
        map.put("salt",user.getSalt());
        map.put("realName",user.getRealName());
        map.put("cardId",user.getCardId());
        map.put("age",user.getAge());
        map.put("sex",user.getSex());
        map.put("phone",user.getPhone());
        map.put("roleId",user.getRoleId());
        map.put("deptId",user.getDeptId());
        map.put("classsifyId",user.getClassifyId());*/
        userMapper.addUser(user);

        return user.getUserId();
    }


    //修改信息前预览
    @Override
    public User preUpdate(Integer userId) {
        User user = userMapper.preUpdate(userId);
        return user;
    }

    //修改员工
    @Transactional
    @Override
    public int updateUser(User user) {
        int i = userMapper.updateUser(user);
        return i;
    }

    //单个删除
    @Transactional
    @Override
    public int deleteUserEmpById(Integer userId) {
        int i = userMapper.deleteUserEmpById(userId);
        return i;
    }

    //批量删除
    @Transactional
    @Override
    public void deleteUserEmpMany(List<Integer> id) {
         userMapper.deleteUserEmpMany(id);

    }

    //导出excle
    @Override
    public List<User> getExcle(List<Integer> list) {
        List<User> excle = userMapper.getExcle(list);

        return excle;
    }

    //异步校验用户名
    @Override
    public Integer ajaxUserId(String username) {
        Integer userId = userMapper.ajaxUserId(username);
        return userId;
    }

    @Override
    public User lookUser(Integer userId) {
        User user = userMapper.preUpdate(userId);
        return user;
    }


    //根据角色编号查询用户姓名
    @Override
    public String findUserNameByRoleId(List<Integer> roleId) {

        ArrayList<String> realName = new ArrayList<>();
        for (Integer id : roleId) {
            List<String> userNameByRoleId = userMapper.findUserNameByRoleId(id);
            if (userNameByRoleId.size()>0){
                for (String s : userNameByRoleId) {
                    realName.add(s);
                }
            }
        }

        StringBuilder stringBuilder = new StringBuilder();

        for (String s : realName) {
            stringBuilder.append(s+"、");
        }

        return stringBuilder.toString();
    }
}
